{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From D:\\Program Files\\anaconda\\lib\\site-packages\\tensorflow\\python\\compat\\v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "non-resource variables are not supported in the long term\n",
      "第0次优化的权重为1.717033，偏置为-0.216528\n",
      "第1次优化的权重为1.505036，偏置为-0.313502\n",
      "第2次优化的权重为1.393122，偏置为-0.339648\n",
      "第3次优化的权重为1.332081，偏置为-0.324580\n",
      "第4次优化的权重为1.296799，偏置为-0.293438\n",
      "第5次优化的权重为1.260492，偏置为-0.273542\n",
      "第6次优化的权重为1.246165，偏置为-0.240978\n",
      "第7次优化的权重为1.227636，偏置为-0.208707\n",
      "第8次优化的权重为1.196190，偏置为-0.193347\n",
      "第9次优化的权重为1.177189，偏置为-0.161121\n",
      "第10次优化的权重为1.162116，偏置为-0.131013\n",
      "第11次优化的权重为1.147244，偏置为-0.104788\n",
      "第12次优化的权重为1.121849，偏置为-0.077638\n",
      "第13次优化的权重为1.106779，偏置为-0.058973\n",
      "第14次优化的权重为1.093573，偏置为-0.031973\n",
      "第15次优化的权重为1.075818，偏置为-0.006170\n",
      "第16次优化的权重为1.061527，偏置为0.020771\n",
      "第17次优化的权重为1.052996，偏置为0.045106\n",
      "第18次优化的权重为1.034237，偏置为0.060153\n",
      "第19次优化的权重为1.027681，偏置为0.093387\n",
      "第20次优化的权重为1.009769，偏置为0.106921\n",
      "第21次优化的权重为0.995974，偏置为0.125004\n",
      "第22次优化的权重为0.979370，偏置为0.141861\n",
      "第23次优化的权重为0.971579，偏置为0.159592\n",
      "第24次优化的权重为0.946441，偏置为0.166681\n",
      "第25次优化的权重为0.941534，偏置为0.188344\n",
      "第26次优化的权重为0.927938，偏置为0.200823\n",
      "第27次优化的权重为0.910006，偏置为0.216124\n",
      "第28次优化的权重为0.905931，偏置为0.234479\n",
      "第29次优化的权重为0.890869，偏置为0.246398\n",
      "第30次优化的权重为0.885826，偏置为0.265893\n",
      "第31次优化的权重为0.857542，偏置为0.270948\n",
      "第32次优化的权重为0.852980，偏置为0.289391\n",
      "第33次优化的权重为0.843765，偏置为0.304164\n",
      "第34次优化的权重为0.839637，偏置为0.319966\n",
      "第35次优化的权重为0.829212，偏置为0.331417\n",
      "第36次优化的权重为0.826608，偏置为0.348897\n",
      "第37次优化的权重为0.817860，偏置为0.360974\n",
      "第38次优化的权重为0.800763，偏置为0.367022\n",
      "第39次优化的权重为0.797155，偏置为0.379889\n",
      "第40次优化的权重为0.791865，偏置为0.391282\n",
      "第41次优化的权重为0.783385，偏置为0.401630\n",
      "第42次优化的权重为0.779841，偏置为0.414536\n",
      "第43次优化的权重为0.773879，偏置为0.423863\n",
      "第44次优化的权重为0.764961，偏置为0.429212\n",
      "第45次优化的权重为0.750539，偏置为0.435804\n",
      "第46次优化的权重为0.749135，偏置为0.450730\n",
      "第47次优化的权重为0.743707，偏置为0.457934\n",
      "第48次优化的权重为0.737665，偏置为0.467387\n",
      "第49次优化的权重为0.732415，偏置为0.478003\n",
      "第50次优化的权重为0.728286，偏置为0.487248\n",
      "第51次优化的权重为0.721192，偏置为0.495522\n",
      "第52次优化的权重为0.720391，偏置为0.507176\n",
      "第53次优化的权重为0.714964，偏置为0.516664\n",
      "第54次优化的权重为0.704440，偏置为0.520729\n",
      "第55次优化的权重为0.696985，偏置为0.527557\n",
      "第56次优化的权重为0.693518，偏置为0.538025\n",
      "第57次优化的权重为0.687755，偏置为0.546356\n",
      "第58次优化的权重为0.682221，偏置为0.553929\n",
      "第59次优化的权重为0.676390，偏置为0.560052\n",
      "第60次优化的权重为0.670463，偏置为0.566321\n",
      "第61次优化的权重为0.663378，偏置为0.572142\n",
      "第62次优化的权重为0.657638，偏置为0.577316\n",
      "第63次优化的权重为0.653329，偏置为0.581017\n",
      "第64次优化的权重为0.651622，偏置为0.587453\n",
      "第65次优化的权重为0.646120，偏置为0.590797\n",
      "第66次优化的权重为0.643928，偏置为0.596742\n",
      "第67次优化的权重为0.641691，偏置为0.602539\n",
      "第68次优化的权重为0.638620，偏置为0.607280\n",
      "第69次优化的权重为0.636072，偏置为0.613923\n",
      "第70次优化的权重为0.632521，偏置为0.619162\n",
      "第71次优化的权重为0.626484，偏置为0.623519\n",
      "第72次优化的权重为0.624030，偏置为0.629371\n",
      "第73次优化的权重为0.623247，偏置为0.635604\n",
      "第74次优化的权重为0.619597，偏置为0.639827\n",
      "第75次优化的权重为0.616476，偏置为0.644330\n",
      "第76次优化的权重为0.614159，偏置为0.647475\n",
      "第77次优化的权重为0.610312，偏置为0.651163\n",
      "第78次优化的权重为0.603809，偏置为0.653239\n",
      "第79次优化的权重为0.601479，偏置为0.657154\n",
      "第80次优化的权重为0.598614，偏置为0.660362\n",
      "第81次优化的权重为0.596160，偏置为0.664385\n",
      "第82次优化的权重为0.594479，偏置为0.669092\n",
      "第83次优化的权重为0.590689，偏置为0.672996\n",
      "第84次优化的权重为0.589308，偏置为0.676678\n",
      "第85次优化的权重为0.586758，偏置为0.679488\n",
      "第86次优化的权重为0.584724，偏置为0.683617\n",
      "第87次优化的权重为0.584021，偏置为0.687198\n",
      "第88次优化的权重为0.580666，偏置为0.690127\n",
      "第89次优化的权重为0.578626，偏置为0.693204\n",
      "第90次优化的权重为0.576518，偏置为0.694899\n",
      "第91次优化的权重为0.574794，偏置为0.698517\n",
      "第92次优化的权重为0.572462，偏置为0.700886\n",
      "第93次优化的权重为0.570580，偏置为0.703612\n",
      "第94次优化的权重为0.567071，偏置为0.705299\n",
      "第95次优化的权重为0.565153，偏置为0.708205\n",
      "第96次优化的权重为0.563964，偏置为0.710896\n",
      "第97次优化的权重为0.561427，偏置为0.712926\n",
      "第98次优化的权重为0.559693，偏置为0.715185\n",
      "第99次优化的权重为0.558425，偏置为0.717903\n",
      "第100次优化的权重为0.557286，偏置为0.720330\n",
      "第101次优化的权重为0.554995，偏置为0.722509\n",
      "第102次优化的权重为0.553002，偏置为0.724504\n",
      "第103次优化的权重为0.552703，偏置为0.727159\n",
      "第104次优化的权重为0.551632，偏置为0.728580\n",
      "第105次优化的权重为0.550549，偏置为0.730585\n",
      "第106次优化的权重为0.547680，偏置为0.731692\n",
      "第107次优化的权重为0.547315，偏置为0.734127\n",
      "第108次优化的权重为0.546975，偏置为0.736344\n",
      "第109次优化的权重为0.544709，偏置为0.737742\n",
      "第110次优化的权重为0.543549，偏置为0.740056\n",
      "第111次优化的权重为0.543085，偏置为0.741997\n",
      "第112次优化的权重为0.541271，偏置为0.743292\n",
      "第113次优化的权重为0.539455，偏置为0.744385\n",
      "第114次优化的权重为0.538363，偏置为0.745739\n",
      "第115次优化的权重为0.537118，偏置为0.746975\n",
      "第116次优化的权重为0.535907，偏置为0.748154\n",
      "第117次优化的权重为0.535759，偏置为0.750193\n",
      "第118次优化的权重为0.535021，偏置为0.751546\n",
      "第119次优化的权重为0.534069，偏置为0.753048\n",
      "第120次优化的权重为0.532739，偏置为0.754076\n",
      "第121次优化的权重为0.531784，偏置为0.755118\n",
      "第122次优化的权重为0.531234，偏置为0.756494\n",
      "第123次优化的权重为0.530617，偏置为0.757840\n",
      "第124次优化的权重为0.529850，偏置为0.759163\n",
      "第125次优化的权重为0.528753，偏置为0.760057\n",
      "第126次优化的权重为0.528170，偏置为0.761156\n",
      "第127次优化的权重为0.527466，偏置为0.762613\n",
      "第128次优化的权重为0.526030，偏置为0.762779\n",
      "第129次优化的权重为0.525610，偏置为0.763902\n",
      "第130次优化的权重为0.525048，偏置为0.764859\n",
      "第131次优化的权重为0.524400，偏置为0.765840\n",
      "第132次优化的权重为0.523784，偏置为0.766798\n",
      "第133次优化的权重为0.523305，偏置为0.767828\n",
      "第134次优化的权重为0.522374，偏置为0.768525\n",
      "第135次优化的权重为0.521639，偏置为0.769260\n",
      "第136次优化的权重为0.521322，偏置为0.770224\n",
      "第137次优化的权重为0.520605，偏置为0.771116\n",
      "第138次优化的权重为0.519985，偏置为0.771841\n",
      "第139次优化的权重为0.518940，偏置为0.772218\n",
      "第140次优化的权重为0.518695，偏置为0.773085\n",
      "第141次优化的权重为0.518600，偏置为0.774146\n",
      "第142次优化的权重为0.518259，偏置为0.774904\n",
      "第143次优化的权重为0.517635，偏置为0.775573\n",
      "第144次优化的权重为0.516831，偏置为0.776054\n",
      "第145次优化的权重为0.516889，偏置为0.776965\n",
      "第146次优化的权重为0.516328，偏置为0.777680\n",
      "第147次优化的权重为0.515812，偏置为0.778171\n",
      "第148次优化的权重为0.515196，偏置为0.778655\n",
      "第149次优化的权重为0.514832，偏置为0.779331\n",
      "第150次优化的权重为0.514431，偏置为0.779904\n",
      "第151次优化的权重为0.514091，偏置为0.780448\n",
      "第152次优化的权重为0.513566，偏置为0.780918\n",
      "第153次优化的权重为0.513186，偏置为0.781618\n",
      "第154次优化的权重为0.512846，偏置为0.782110\n",
      "第155次优化的权重为0.512536，偏置为0.782644\n",
      "第156次优化的权重为0.512246，偏置为0.783027\n",
      "第157次优化的权重为0.512046，偏置为0.783617\n",
      "第158次优化的权重为0.511768，偏置为0.784033\n",
      "第159次优化的权重为0.511281，偏置为0.784303\n",
      "第160次优化的权重为0.510960，偏置为0.784720\n",
      "第161次优化的权重为0.510732，偏置为0.785121\n",
      "第162次优化的权重为0.510496，偏置为0.785554\n",
      "第163次优化的权重为0.510115，偏置为0.785891\n",
      "第164次优化的权重为0.509838，偏置为0.786263\n",
      "第165次优化的权重为0.509704，偏置为0.786702\n",
      "第166次优化的权重为0.509346，偏置为0.787006\n",
      "第167次优化的权重为0.509132，偏置为0.787326\n",
      "第168次优化的权重为0.508822，偏置为0.787600\n",
      "第169次优化的权重为0.508621，偏置为0.787942\n",
      "第170次优化的权重为0.508530，偏置为0.788373\n",
      "第171次优化的权重为0.508110，偏置为0.788545\n",
      "第172次优化的权重为0.508093，偏置为0.788974\n",
      "第173次优化的权重为0.507733，偏置为0.789198\n",
      "第174次优化的权重为0.507480，偏置为0.789436\n",
      "第175次优化的权重为0.507254，偏置为0.789689\n",
      "第176次优化的权重为0.507170，偏置为0.790063\n",
      "第177次优化的权重为0.506959，偏置为0.790300\n",
      "第178次优化的权重为0.506785，偏置为0.790576\n",
      "第179次优化的权重为0.506587，偏置为0.790843\n",
      "第180次优化的权重为0.506409，偏置为0.791092\n",
      "第181次优化的权重为0.506301，偏置为0.791383\n",
      "第182次优化的权重为0.506084，偏置为0.791586\n",
      "第183次优化的权重为0.505977，偏置为0.791821\n",
      "第184次优化的权重为0.505776，偏置为0.792052\n",
      "第185次优化的权重为0.505658，偏置为0.792270\n",
      "第186次优化的权重为0.505460，偏置为0.792435\n",
      "第187次优化的权重为0.505206，偏置为0.792578\n",
      "第188次优化的权重为0.505031，偏置为0.792759\n",
      "第189次优化的权重为0.504876，偏置为0.792939\n",
      "第190次优化的权重为0.504823，偏置为0.793183\n",
      "第191次优化的权重为0.504816，偏置为0.793476\n",
      "第192次优化的权重为0.504663，偏置为0.793627\n",
      "第193次优化的权重为0.504465，偏置为0.793749\n",
      "第194次优化的权重为0.504353，偏置为0.793944\n",
      "第195次优化的权重为0.504305，偏置为0.794162\n",
      "第196次优化的权重为0.504199，偏置为0.794319\n",
      "第197次优化的权重为0.504041，偏置为0.794441\n",
      "第198次优化的权重为0.503950，偏置为0.794595\n",
      "第199次优化的权重为0.503824，偏置为0.794723\n"
     ]
    }
   ],
   "source": [
    "import tensorflow.compat.v1 as tf\n",
    "tf.disable_v2_behavior()\n",
    "\n",
    "\n",
    "x = tf.random_normal([200, 1], mean=1.2, stddev=0.6, name=\"x\")\n",
    "y = tf.matmul(x, [[0.5]]) + 0.8\n",
    "\n",
    "# 建立线性回归模型\n",
    "weight = tf.Variable(tf.random_normal([1, 1], mean=0, stddev=1.0), name=\"weight\")\n",
    "bais = tf.Variable(0.0, name=\"bais\")\n",
    "# 预测值\n",
    "y_predict = tf.matmul(x, weight) + bais\n",
    "\n",
    "# 损失函数，均方误差\n",
    "loss = tf.reduce_mean(tf.square(y - y_predict))\n",
    "\n",
    "# 梯度下降优化损失\n",
    "train_op = tf.train.GradientDescentOptimizer(0.1).minimize(loss)\n",
    "\n",
    "# 初始化op\n",
    "init_op = tf.global_variables_initializer()\n",
    "\n",
    "# 运行\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init_op)\n",
    "\n",
    "    for i in range(200):\n",
    "        sess.run(train_op)\n",
    "        print(\"第%d次优化的权重为%f，偏置为%f\" % (i, weight.eval(), bais.eval()))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
